package com.tigercel.xiangchengyun.adapter

import android.support.v4.content.ContextCompat
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.tigercel.xiangchengyun.R
import com.tigercel.xiangchengyun.entity.ChannelSub
import kotlinx.android.synthetic.main.item_channel_sub.view.*

class ChannelSubAdapter(private val channels: List<ChannelSub>,
                        private val itemClick: (ChannelSub) ->Unit) :
        RecyclerView.Adapter<ChannelSubAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder? {
        val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.item_channel_sub, parent, false)
        return ViewHolder(view ,itemClick)
    }

    override fun getItemCount(): Int = channels.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindChannelSub(channels[position])

    }

    class ViewHolder(view: View, private val itemClick: (ChannelSub) -> Unit) :
            RecyclerView.ViewHolder(view)
    {
        fun bindChannelSub(channel: ChannelSub) {
            with(channel) {
                itemView.name.text = name
                itemView.desc.text = desc
                itemView.sub.text = if (isSubscribe) "已订" else "订阅"

                itemView.ll_sub.setOnClickListener {
                    if (isSubscribe) {
                        itemView.ll_sub.isSelected = true
                        itemView.sub.setTextColor(ContextCompat.getColor(itemView.context, R.color.colorPrimary))
                        itemView.sub.text = "订阅"
                    } else {
                        itemView.ll_sub.isSelected = false
                        itemView.sub.setTextColor(ContextCompat.getColor(itemView.context, R.color.text_color_support))
                        itemView.sub.text = "已订"
                    }
                    isSubscribe = !isSubscribe
                }
            }
        }

    }

}